import axios from 'axios';
import { ElMessage } from 'element-plus';
import { BASE_URL, TIMEOUT } from './config';
const request = axios.create({
  baseURL: BASE_URL,
  timeout: TIMEOUT,
});
// 请求拦截 :添加token、headers
request.interceptors.request.use((config) => {
  return config;
});
// 响应拦截
request.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (err) => {
    const status = err.response?.status;
    console.log(status);

    switch (status) {
      case 404:
        ElMessage({
          type: 'error',
          message: '请求失败路径出现问题',
        });
        break;
      case 500 | 501 | 502 | 503 | 505:
        ElMessage({
          type: 'error',
          message: '服务器挂了～',
        });
        break;
      case 401:
        ElMessage({
          type: 'error',
          message: '参数有误！',
        });
        break;
    }
    return Promise.reject(new Error(err.message));
  },
);

export default request;
